package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.redis.GmallCache;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: atguigu
 * @create: 2023-08-30 14:31
 */
@Service
public class SpuManageServiceImpl implements SpuManageService {

    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuPosterService spuPosterService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;


    /**
     * 分页查询商品SPU列表
     *
     * @param spuInfoPage
     * @param category3Id
     * @return
     */
    @Override
    public Page<SpuInfo> getSpuByPage(Page<SpuInfo> spuInfoPage, Long category3Id) {
        //1.构建条件对象
        LambdaQueryWrapper<SpuInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuInfo::getCategory3Id, category3Id);
        //TODO 新增SKU使用SPU所属品牌ID，故需要增加品牌ID列
        queryWrapper.select(SpuInfo::getId, SpuInfo::getSpuName, SpuInfo::getDescription, SpuInfo::getTmId);
        queryWrapper.orderByDesc(SpuInfo::getUpdateTime);
        //2.执行分页查询
        return spuInfoService.page(spuInfoPage, queryWrapper);
    }


    /**
     * 商品SPU保存
     * 1.将商品基本信息封装SpuInfo对象 存入spu_info表
     * 2.将商品图片列表封装SpuImage集合 批量存入spu_image表
     * 3.将商品海报列表封装SpuPost集合 批量存入spu_poster表
     * 4.将商品销售属性列表封装SpuSaleAttr集合 批量存入spu_sale_attr表
     * 5.将某个销售包含属性值列表封装SpuSaleAttrValue集合 批量存入spu_sale_attr_value表
     *
     * @param spuInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
        //1.将商品基本信息封装SpuInfo对象 存入spu_info表
        spuInfoService.save(spuInfo);
        Long spuId = spuInfo.getId();

        //2.将商品图片列表封装SpuImage集合 批量存入spu_image表
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)) {
            spuImageList.forEach(spuImage -> {
                //将商品图片关联到SPU
                spuImage.setSpuId(spuId);
            });
            spuImageService.saveBatch(spuImageList);
        }

        //3.将商品海报列表封装SpuPost集合 批量存入spu_poster表
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuPosterList)) {
            spuPosterList.forEach(spuImage -> {
                //将海报图片关联到SPU
                spuImage.setSpuId(spuId);
            });
            spuPosterService.saveBatch(spuPosterList);
        }

        //4.将商品销售属性列表封装SpuSaleAttr集合 批量存入spu_sale_attr表
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)) {
            for (SpuSaleAttr spuSaleAttr : spuSaleAttrList) {
                //销售属性关联商品SPUID
                spuSaleAttr.setSpuId(spuId);
                //5.将某个销售包含属性值列表封装SpuSaleAttrValue集合 批量存入spu_sale_attr_value表
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)) {
                    for (SpuSaleAttrValue spuSaleAttrValue : spuSaleAttrValueList) {
                        //销售属性值关联商品
                        spuSaleAttrValue.setSpuId(spuId);
                        //销售属性值设置销售属性名称“冗余”
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                    }
                    spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
                }
            }
            spuSaleAttrService.saveBatch(spuSaleAttrList);
        }
    }

    /**
     * 根据spuId 获取spu图片集合
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SpuImage> getSpuImageList(Long spuId) {
        LambdaQueryWrapper<SpuImage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuImage::getSpuId, spuId);
        return spuImageService.list(queryWrapper);
    }


    /**
     * 根据spuId 查询销售属性
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
        //通过spu销售属性业务层直接获取对应持久层接口
        SpuSaleAttrMapper spuSaleAttrMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
        return spuSaleAttrMapper.getSpuSaleAttrList(spuId);
    }


    /**
     * 根据SpuId查询海报图片列表
     *
     * @param spuId
     * @return
     */
    @Override
    @GmallCache(prefix = "spuPoster:")
    public List<SpuPoster> getSpuPosterBySpuId(Long spuId) {
        LambdaQueryWrapper<SpuPoster> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuPoster::getSpuId, spuId);
        return spuPosterService.list(queryWrapper);
    }

    /**
     * SPU商品所有销售属性以及属性值，根据SKUID将当前SKU销售属性值选中
     *
     * @param skuId
     * @param spuId
     * @return
     */
    @Override
    @GmallCache(prefix = "spuSaleAttr:")
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(Long skuId, Long spuId) {
        SpuSaleAttrMapper spuSaleAttrMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
        return spuSaleAttrMapper.getSpuSaleAttrListCheckBySku(skuId, spuId);
    }
}
